package com.link2room.optimus.facade.impl;

import com.link2room.common.entity.BaseEntity;
import com.link2room.common.enums.BaseSysExceptionEnum;
import com.link2room.common.jpa.condition.ConditionDetail;
import com.link2room.common.jpa.condition.L2RCondition;
import com.link2room.common.jpa.criteria.CriteriaUtil;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.StringUtil;
import com.link2room.enums.exception.OptimusSysExceptionEnum;
import com.link2room.optimus.entity.TaobaoXrateplan;
import com.link2room.optimus.exception.OptimusException;
import com.link2room.optimus.constant.BaseRateplanConstant;
import com.link2room.optimus.dto.param.base.TaobaoXrateplanBaseParamDto;
import com.link2room.optimus.dto.result.base.TaobaoXrateplanDto;
import com.link2room.optimus.facade.ITaobaoXrateplanFacadeService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Created by sss on 2016/9/1.
 */
@Component("taobaoXrateplanFacadeService")
public class TaobaoXrateplanFacadeServiceImpl implements ITaobaoXrateplanFacadeService {
    @Resource
    private ICommonService commonService;

    /**
     * 保存飞猪价格计划
     * @param taobaoXrateplanBaseParamDto
     * @throws Exception
     */
    @Override
    public TaobaoXrateplanDto saveOrUpdateTaobaoXrateplan(TaobaoXrateplanBaseParamDto taobaoXrateplanBaseParamDto)throws Exception{
        if(taobaoXrateplanBaseParamDto.getTaobaoXrateplanDto()!=null){
            TaobaoXrateplan taobaoXrateplan = ClassConverUtil.copyProperties(taobaoXrateplanBaseParamDto.getTaobaoXrateplanDto(), TaobaoXrateplan.class);
            if(BaseRateplanConstant.RESTYPE.TAOBAO_PRE.equals(taobaoXrateplan.getResType())){
                taobaoXrateplan.setPaymentType(1L);
            }else if(BaseRateplanConstant.RESTYPE.TAOBAO_CREDIT.equals(taobaoXrateplan.getResType())){
                taobaoXrateplan.setPaymentType(6L);
            }else {
                throw new OptimusException(OptimusSysExceptionEnum.PAYTYPE_NOT_EXIT, taobaoXrateplanBaseParamDto.toString());
            }
            commonService.saveOrUpdate(taobaoXrateplan);
            return ClassConverUtil.copyPropertiesToDto(taobaoXrateplan, TaobaoXrateplanDto.class,false);
        }else {
            throw new OptimusException(BaseSysExceptionEnum.PARAM_IS_NULL, taobaoXrateplanBaseParamDto.toString());
        }
    }

    /**
     * 飞猪价格计划列表
     * @param taobaoXrateplanBaseParamDto
     * @throws Exception
     */
    @Override
    public QueryResult<TaobaoXrateplanDto> listTaobaoXrateplans(TaobaoXrateplanBaseParamDto taobaoXrateplanBaseParamDto)throws Exception{
        L2RCondition l2RCondition=new L2RCondition();
        ConditionDetail[] conditionDetails = new ConditionDetail[2];
        conditionDetails[0]= new ConditionDetail(BaseEntity.BaseEntity_.lhotelGroupCode.toString(), CriteriaUtil.Operator.EQ, taobaoXrateplanBaseParamDto.getLhotelGroupCode());
        if(StringUtil.isNotBlank(taobaoXrateplanBaseParamDto.getMcCode())){
            conditionDetails[1]= new ConditionDetail(TaobaoXrateplan.TaobaoXrateplan_.rpCode.toString(), CriteriaUtil.Operator.LIKE, taobaoXrateplanBaseParamDto.getMcCode() + "%");
        }else{
            conditionDetails[1]= new ConditionDetail(TaobaoXrateplan.TaobaoXrateplan_.rpCode.toString(), CriteriaUtil.Operator.EQ, taobaoXrateplanBaseParamDto.getRpCode() );
        }
        l2RCondition.setAndConditions(conditionDetails);

        QueryResult<TaobaoXrateplan> taobaoXrateplanQueryResult = commonService.findAllByCondition(TaobaoXrateplan.class,
                l2RCondition, taobaoXrateplanBaseParamDto.getCurrentPage(), taobaoXrateplanBaseParamDto.getPageSize());
        return ClassConverUtil.copyQueryResultToDtoQueryResult(taobaoXrateplanQueryResult, TaobaoXrateplanDto.class, false);
    }

    /**
     * 飞猪单个价格计划
     * @param taobaoXrateplanBaseParamDto
     * @throws Exception
     */
    @Override
    public TaobaoXrateplanDto findTaobaoXrateplanById(TaobaoXrateplanBaseParamDto taobaoXrateplanBaseParamDto)throws Exception{
        TaobaoXrateplan taobaoXrateplan = commonService.findOneById(TaobaoXrateplan.class, taobaoXrateplanBaseParamDto.getId());
        return ClassConverUtil.copyPropertiesToDto(taobaoXrateplan,TaobaoXrateplanDto.class, false);
    }

    /**
     * 飞猪单个价格计划
     * @param taobaoXrateplanBaseParamDto
     * @throws Exception
     */
    @Override
    public TaobaoXrateplanDto findTaobaoXrateplanByRpcode(TaobaoXrateplanBaseParamDto taobaoXrateplanBaseParamDto)throws Exception{
        TaobaoXrateplan queryTRP=new TaobaoXrateplan();
        queryTRP.setLhotelGroupCode(taobaoXrateplanBaseParamDto.getLhotelGroupCode());
        queryTRP.setRpCode(taobaoXrateplanBaseParamDto.getRpCode());
        TaobaoXrateplan taobaoXrateplan = commonService.findOneEQ(TaobaoXrateplan.class, queryTRP);
        return ClassConverUtil.copyPropertiesToDto(taobaoXrateplan,TaobaoXrateplanDto.class, false);
    }
}
